﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;

namespace Smatime.Web.Models
{
    public class CompanyStaff : BaseEntity
    {
        public CompanyStaff()
        {
            Id = -1;
            CompanyId = -1;
            Name = null;
            NameKana = null;
            FaceModel1 = null;
            FaceModel2 = null;
            WorkingStatus = 1;
            RowStatus = 1;
            ContractType = -1;
            WorkVisa = 1;
            VisaExperationDate = null;
            StoreIds = new long[0];
        }

        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public long CompanyId { get; set; }
        public string Name { get; set; }
        public string NameKana { get; set; }
        public string FaceModel1 { get; set; }
        public string FaceModel2 { get; set; }
        public int WorkingStatus { get; set; }
        public int ContractType { get; set; }
        public int WorkVisa { get; set; }
        public DateTime? VisaExperationDate { get; set; }
        public long[] StoreIds { get; set; }
        public override BaseEntity FromDataRow(DataRow row)
        {
            var listListProperties = GetType().GetProperties()
                .Where(
                    propertyInfo => (row.Table.Columns.Contains(propertyInfo.Name)) &&
                                    (row[propertyInfo.Name].HasValue()) &&
                                    (!propertyInfo.Name.Equals("StoreIds")));
            foreach (var propertyInfo in listListProperties)
            {
                propertyInfo.SetValue(this, row[propertyInfo.Name]);
            }
            return base.FromDataRow(row);
        }

        public override void ToDataRow(DataRow row)
        {
            var listListProperties = GetType().GetProperties()
                .Where(propertyInfo =>
                    (propertyInfo.GetValue(this).HasValue())
                    && (!propertyInfo.Name.Equals("StoreIds")));
            foreach (var propertyInfo in listListProperties)
            {
                row[propertyInfo.Name] = propertyInfo.GetValue(this);
            }
            base.ToDataRow(row);
        }
    }
}